package Demo01;

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

class IntCompare implements Comparator<Integer> {
    @Override
    public int compare(Integer o1, Integer o2) {
        return o2.compareTo(o1);
    }
}
public class Test {
    public static void main(String[] args) {
        Heap heap = new Heap();
        int[] arr = {1,3,7,1,3,2,6,1,7,4};
        heap.initArray(arr);
        heap.createHeap();
        heap.heapSort();
        System.out.println(Arrays.toString(heap.elements));
    }
/*    public static void main03(String[] args) {
        int[] arr = {1,3,7,2,6};
        Test test = new Test();
        System.out.println(test.smallestK(arr, 3));
    }*/
    public static void main02(String[] args) {
        IntCompare intCompare = new IntCompare();
        PriorityQueue priorityQueue = new PriorityQueue(intCompare);
        priorityQueue.offer(1);
        priorityQueue.offer(2);
        priorityQueue.offer(3);

        System.out.println(priorityQueue.poll());
        System.out.println(priorityQueue.poll());

    }
    public static void main01(String[] args) {
        Heap heap = new Heap();
        int[] arr = { 27,15,19,18,28,34,65,49,25,37 };
        heap.initArray(arr);
        heap.createHeap();
        System.out.println(heap.pop());
//        heap.insertHeap(80);
        System.out.println("=================");
    }
}

class Solution {
    public int[] smallestK(int[] arr, int k) {
        int[] array = new int[k];
        if(arr.length==0||k==0) {
            return array;
        }
        if (arr.length <= k) {
            return arr;
        }
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2.compareTo(o1);
            }
        });
        for (int i = 0; i < k; i++) {
            priorityQueue.offer(arr[i]);
        }
        int len = arr.length;
        for (int i = k; i < len; i++) {
            if (arr[i] < priorityQueue.peek()) {
                priorityQueue.poll();
                priorityQueue.offer(arr[i]);
            }
        }

        for (int i = 0; i < k; i++) {
            array[i] = priorityQueue.poll();
        }
        return array;
    }
}